BX.namespace("BX.Iblock");BX.Iblock.IblockElementSelector=function(){var e=function(e){this.jsObject=e.jsObject;this.selectorId=e.selectorId;this.searchInputId=e.searchInputId;this.panelSelectedValues=e.panelSelectedValues==="Y";this.popup=e.popup==="Y";this.multiple=e.multiple==="Y";this.iblockId=e.iblockId;this.onChange=e.onChange;this.onSelect=e.onSelect;this.onUnSelect=e.onUnSelect;this.currentElements=e.currentElements;this.lastElements=e.lastElements;this.inputName=e.inputName;this.onlyRead=e.onlyRead==="Y";this.adminSection=e.admin==="Y"?"Y":"N";this.templateUrl=e.templateUrl;this.init()};e.prototype.init=function(){if(!this.selectorId){return false}this.ajaxUrl="/bitrix/components/bitrix/iblock.element.selector/ajax.php";this.listElementsData={};this.selectedElements=[];this.popupWindow=null;var e;if(this.currentElements.length){BX.addClass(BX(this.selectorId+"_search"),"ies-content-find-content-selected");var t=[];for(e in this.currentElements){if(this.currentElements.hasOwnProperty(e)){this.selectedElements[this.currentElements[e].ID]={id:this.currentElements[e].ID,name:this.currentElements[e].NAME,url:this.currentElements[e].URL};t.push({id:this.currentElements[e].ID,name:this.currentElements[e].NAME,url:this.currentElements[e].URL})}}this.setSelected(t)}if(this.lastElements.length){for(e in this.lastElements){if(this.lastElements.hasOwnProperty(e)){this.listElementsData[this.lastElements[e].ID]={id:this.lastElements[e].ID,name:this.lastElements[e].NAME,url:this.lastElements[e].URL}}}}if(BX(this.searchInputId)){this.searchInput=BX(this.searchInputId)}else{this.searchInput=BX(this.selectorId+"_search_input")}if(this.searchInput){BX.bind(this.searchInput,"keyup",BX.proxy(this.search,this));BX.bind(this.searchInput,"focus",BX.proxy(this._onFocus,this))}if(this.popup){BX.bind(BX(this.selectorId+"_select_button"),"click",BX.proxy(this.showSelector,this))}if(this.panelSelectedValues){this.onChange=BX.proxy(this.setToSelectedValues,this)}if(this.onChange){this.onChange(this.selectedElements)}};e.prototype.search=function(){this.requestTimeout=clearTimeout(this.requestTimeout);if(typeof this.searchRequest==="object"){this.searchRequest.abort();this.searchRequest=false}if(this.searchInput.value.length>0){this.displayTab("search");BX.addClass(BX(this.selectorId+"_search"),"ies-content-find-content-selected");this.url=this.ajaxUrl+"?sessid="+BX.bitrix_sessid()+"&mode=search&iblockId="+this.iblockId+"&admin="+this.adminSection+"&string="+encodeURIComponent(this.searchInput.value);this.requestTimeout=setTimeout(BX.proxy(this.request,this),400)}};e.prototype.displayTab=function(e){BX.removeClass(BX(this.selectorId+"_last"),"ies-content-tab-content-selected");BX.removeClass(BX(this.selectorId+"_search"),"ies-content-tab-content-selected");BX.addClass(BX(this.selectorId+"_"+e),"ies-content-tab-content-selected");BX.removeClass(BX(this.selectorId+"_tab_last"),"ies-content-tab-selected");BX.removeClass(BX(this.selectorId+"_tab_search"),"ies-content-tab-selected");BX.addClass(BX(this.selectorId+"_tab_"+e),"ies-content-tab-selected");if(e==="search")this.searchInput.focus()};e.prototype._onFocus=function(){this.searchInput.value=""};e.prototype.showResult=function(e){var t=BX(this.selectorId+"_search");if(!e||!e.length){t.innerHTML="";return}var s=e;if(t){t.innerHTML="";var i=BX.create("table",{props:{className:"ies-content-columns",cellspacing:"0"},children:[BX.create("tbody")]});var l=BX.create("tr");i.firstChild.appendChild(l);var n=BX.create("td");l.appendChild(n);t.appendChild(i);for(var a=0;a<s.length;a++){var c;var r=false;this.listElementsData[s[a].ID]={id:s[a].ID,name:s[a].NAME,url:s[a].URL};var h=BX.create("input",{props:{className:"ies-hidden-input"}});if(this.multiple){h.name=this.selectorId+"[]";h.type="checkbox"}else{h.name=this.selectorId;h.type="radio"}var o=document.getElementsByName(h.name);var d=0;while(!r&&d<o.length){if(o[d].value===s[a].ID&&o[d].checked){r=true}d++}h.value=s[a].ID;var u=s[a].NAME;c=BX.create("div",{props:{className:"ies-content-item"+(r?" ies-content-item-selected":""),id:"ies-anchor_element_id_"+parseInt(s[a].ID)},events:{click:BX.proxy(this.select,this)},children:[h,BX.create("div",{props:{className:"ies-content-item-text"},text:u}),BX.create("div",{props:{className:"ies-content-item-icon"}})]});n.appendChild(c);if(a===Math.ceil(s.length/2)-1){n=BX.create("td");i.firstChild.appendChild(n)}}}};e.prototype.select=function(e){var t=null;if(e.currentTarget){t=e.currentTarget}var s=BX.findChild(t,{tag:"input"});var i,l,n;if(!this.multiple){l=document.getElementsByName(this.selectorId);for(n=0;n<l.length;n++){if(l[n].value!==s.value){BX.removeClass(l[n].parentNode,"ies-content-item-selected")}else{BX.addClass(l[n].parentNode,"ies-content-item-selected")}}s.checked=true;BX.addClass(t,"ies-content-item-selected");this.searchInput.value=this.listElementsData[s.value].name;this.selectedElements=[];this.selectedElements[s.value]={id:s.value,name:this.listElementsData[s.value].name,url:this.listElementsData[s.value].url};if(BX(this.selectorId+"_hidden_values")){BX(this.selectorId+"_hidden_values").innerHTML=""}}else{l=document.getElementsByName(this.selectorId+"[]");for(n=0;n<l.length;n++){if(l[n].value===s.value){l[n].checked=false;BX.toggleClass(l[n].parentNode,"ies-content-item-selected")}}if(BX.hasClass(s.parentNode,"ies-content-item-selected")){s.checked=true}if(s.checked){var a=BX.findChild(BX(this.selectorId+"_selected_elements"),{className:"ies-content-selected-items"});if(!BX(this.selectorId+"_element_selected_"+parseInt(s.value))){var c=BX.findChild(t,{tag:"div",className:"ies-content-item-text"},true);var r=BX.create("div",{props:{className:"ies-content-selected-item",id:this.selectorId+"_element_selected_"+parseInt(s.value)},children:[BX.create("div",{props:{id:this.selectorId+"-element-unselect-"+parseInt(s.value),className:"ies-content-selected-item-icon"},attrs:{onclick:'BX.Iblock["'+this.jsObject+'"].unselect("'+parseInt(s.value)+'");'}}),BX.create("span",{props:{className:"ies-content-selected-item-text"},text:c.innerHTML})]});a.appendChild(r);i=BX(this.selectorId+"_current_count");i.innerHTML=parseInt(i.innerHTML)+1;this.selectedElements[s.value]={id:s.value,name:this.listElementsData[s.value].name,url:this.listElementsData[s.value].url}}}else{BX.remove(BX(this.selectorId+"_element_selected_"+parseInt(s.value)));BX.remove(BX(this.selectorId+"_selected_value_"+parseInt(s.value)));i=BX(this.selectorId+"_current_count");i.innerHTML=parseInt(i.innerHTML)-1;this.selectedElements[s.value]=null}}if(this.onSelect){if(this.multiple){this.onSelect(this.selectedElements)}else{var h=this.selectedElements.pop();this.selectedElements.push(h);this.onSelect(h)}}BX.onCustomEvent(this,"on-change",[this.toObject(this.selectedElements)]);if(this.onChange){this.onChange(this.selectedElements)}};e.prototype.unselect=function(e,t){var s=BX(this.selectorId+"-element-unselect-"+e);var i=document.getElementsByName(this.selectorId+(this.multiple?"[]":""));for(var l=0;l<i.length;l++){if(i[l].value===e){i[l].checked=false;BX.removeClass(i[l].parentNode,"ies-content-item-selected")}}if(this.multiple){if(s){BX.remove(s.parentNode)}var n=BX(this.selectorId+"_current_count");n.innerHTML=parseInt(n.innerHTML)-1}this.selectedElements[e]=null;BX.onCustomEvent(this,"un-select",[this.toObject(this.selectedElements)]);if(this.onChange){this.onChange(this.selectedElements)}if(this.onUnSelect&&!t){this.onUnSelect(this.selectedElements)}if(BX(this.selectorId+"_selected_value_"+parseInt(e))){BX(this.selectorId+"_selected_value_"+parseInt(e)).value=0}if(this.searchInput&&!this.multiple){this.searchInput.value=""}};e.prototype.getSelected=function(){return this.selectedElements};e.prototype.setSelected=function(e){var t,s;for(t=0,s=this.selectedElements.length;t<s;t++){if(this.selectedElements[t]&&this.selectedElements[t].id){this.unselect(this.selectedElements[t].id,true)}}if(!e.length){return}this.selectedElements=[];for(t=0,s=e.length;t<s;t++){if(!e[t]||!e[t].id)continue;this.selectedElements[e[t].id]=e[t];if(this.multiple){var i=BX.findChild(BX(this.selectorId+"_selected_elements"),{className:"ies-content-selected-items"});var l=BX.create("div",{props:{className:"ies-content-selected-item",id:this.selectorId+"_element_selected_"+parseInt(e[t].id)},children:[BX.create("div",{props:{id:this.selectorId+"-element-unselect-"+parseInt(e[t].id),className:"ies-content-selected-item-icon"},attrs:{onclick:'BX.Iblock["'+this.jsObject+'"].unselect("'+parseInt(e[t].id)+'");'}}),BX.create("span",{props:{className:"ies-content-selected-item-text"},text:BX.util.htmlspecialchars(e[t].name)})]});i.appendChild(l)}var n=document.getElementsByName(this.selectorId+(this.multiple?"[]":""));for(var a=0;a<n.length;a++){if(n[a].value===e[t].id){if(n[a].parentNode.className==="")continue;BX.toggleClass(n[a].parentNode,"ies-content-item-selected")}}}if(this.multiple){BX.adjust(BX(this.selectorId+"_current_count"),{text:this.getCountElements(e).toString()})}};e.prototype.toObject=function(e){var t={},s;for(s in e){if(e.hasOwnProperty(s)){s=parseInt(s);if(typeof s==="number"&&e[s]!==null){t[s]=BX.clone(e[s])}}}return t};e.prototype.request=function(){var e=(new Date).getTime();this.lastSearchTime=e;this.searchRequest=BX.ajax.loadJSON(this.url,{template_url:this.templateUrl},BX.proxy(function(t){if(this.lastSearchTime===e){this.showResult(t)}},this))};e.prototype.showSelector=function(){if(!this.popupWindow){this.popupWindow=new BX.PopupWindow(this.selectorId+"popup-window",BX(this.selectorId+"_select_button"),{offsetTop:1,autoHide:true,content:BX(this.selectorId),zIndex:3e3})}else{this.popupWindow.setBindElement(this)}if(this.popupWindow.popupContainer.style.display!=="block"){this.popupWindow.show()}};e.prototype.setToSelectedValues=function(e){var t="";for(var s=0;s<e.length;s++){var i=e[s];if(i){if(!BX(this.selectorId+"_selected_value_"+i.id)){BX(this.selectorId+"_hidden_values").appendChild(BX.create("input",{props:{id:this.selectorId+"_selected_value_"+parseInt(i.id)},attrs:{type:"hidden",value:parseInt(i.id),name:this.inputName+(this.multiple?"[]":"")}}))}if(i.url){t+='<a href="'+BX.util.htmlspecialchars(i.url)+'" target="_blank">'+BX.util.htmlspecialchars(i.name)+"</a>"}else{t+=BX.util.htmlspecialchars(i.name)}if(!this.multiple&&!this.onlyRead){t+='<span class="ies-content-delete-icon" onclick="BX.Iblock[\''+this.jsObject+"'].unselect('"+parseInt(i.id)+"')\"></span>"}t+="<br>"}}BX(this.selectorId+"_visible_values").innerHTML=t};e.prototype.getCountElements=function(e){var t=0,s;for(s=0;s<e.length;s++){if(BX.type.isNotEmptyObject(e[s]))t++}return t};return e}();
//# sourceMappingURL=script.map.js